#include "IStateMachine.h"
#include <string.h>


int StateIdValid(int stateId)
{
    return (stateId > 0 && stateId <= (MAX_STATENUM_IN_MACHINE-1));
}

int StateIdActive(TStateMachine* pStateMachine,int stateId)
{
    return (pStateMachine->m_statesFlag[stateId]&0x1);
}

int registerStateToMachine(TStateMachine* pStateMachine,int stateId,TState* pState)
{
    if (!StateIdValid(stateId))
    {
        return -1;
    }
    memcpy(&(pStateMachine->m_states[stateId]),pState,sizeof(TState));
    pStateMachine->m_statesFlag[stateId] = pStateMachine->m_statesFlag[stateId]|0x1;
    return 0;
}

int pushMachine(TStateMachine* pStateMachine,TStatePushMsg* msg)
{
    int iNextStateId = pStateMachine->m_states[pStateMachine->m_curStateId].m_pFunc(msg,&(pStateMachine->m_ctx));
    if(!StateIdValid(iNextStateId))
    {
        return -1;
    }
    if(!StateIdActive(pStateMachine,iNextStateId))
    {
        return -1;
    }
    pStateMachine->m_curStateId = iNextStateId;
    return 0;
}

